import { createRouter, createWebHashHistory } from "vue-router";
import { start, close } from "@/utils/nprogress";
import aiRoutes from "./aiRoutes";
import workflowRoutes from "./workflowRoutes"

const routes = [
    ...aiRoutes,
    {
        path: '/:pathMatch(.*)*',
        name: 'NotFound',
        component: () => import('../view/common/404.vue'),
        meta: {
            auth: true
        },
    },
    {
        path: '/login',
        name: "login",
        component: () => import('@/view/common/login.vue'),
        meta: {
            auth: true
        },
    },
    {
        path: '/header',
        name: "header",
        component: () => import('../view/common/header.vue'),
        meta: {
            auth: false
        },
    },
    {
        path: '/aside',
        name: "aside",
        component: () => import('../view/common/aside.vue'),
        meta: {
            auth: false
        },
    },
    {
        path: '/',
        name: "mian",
        component: () => import('../view/common/main.vue'),
        redirect: "/home",
        meta: {
            auth: false
        },
        children: [
            ...workflowRoutes,
            {
                path: "/home",
                name: "home",
                component: () => import("../view/common/home.vue"),
                meta: {
                    auth: false
                }
            },
            {
                path: "/user",
                name: "user",
                component: () => import("../view/author/user/index.vue"),
                meta: {
                    auth: false
                }
            },
            {
                path: "/role",
                name: "role",
                component: () => import("../view/author/role/index.vue"),
                meta: {
                    auth: false
                }
            },
            {
                path: "/menu",
                name: "menu",
                component: () => import("../view/author/menu/index.vue"),
                meta: {
                    auth: false
                }
            },
            {
                path: "/dept",
                name: "dept",
                component: () => import("../view/author/dept/index.vue"),
                meta: {
                    auth: false
                }
            },
            {
                path: "/post",
                name: "post",
                component: () => import("../view/author/post/index.vue"),
                meta: {
                    auth: false
                }
            },
            {
                path: "/dict",
                name: "dict",
                component: () => import("../view/system/dict/index.vue"),
                meta: {
                    auth: false
                }
            },
            {
                path: "/file",
                name: "file",
                component: () => import("../view/system/file/index.vue"),
                meta: {
                    auth: false
                }
            },
            {
                path: "/generateCode",
                name: "generateCode",
                component: () => import("../view/system/generate/index.vue"),
                meta: {
                    auth: false
                }
            },
            {
                path: "/scheduledJob",
                name: "scheduledJob",
                component: () => import("../view/job/scheduled/index.vue"),
                meta: {
                    auth: false
                }
            },
            {
                path: "/jobLogs",
                name: "jobLogs",
                component: () => import("../view/job/jobLogs/index.vue"),
                meta: {
                    auth: false
                }
            },
            {
                path: "/errorLogs",
                name: "errorLogs",
                component: () => import("../view/system/errorLogs/index.vue"),
                meta: {
                    auth: false
                }
            },
            {
                path: "/loginLogs",
                name: "loginLogs",
                component: () => import("../view/system/loginLogs/index.vue"),
                meta: {
                    auth: false
                }
            },
            {
                path: "/cache",
                name: "cache",
                component: () => import("../view/system/cache/index.vue"),
                meta: {
                    auth: false
                }
            },
            {
                path: "/dataSync",
                name: "dataSync",
                component: () => import("../view/system/dataSync/index.vue"),
                meta: {
                    auth: false
                }
            },

        ]
    }
]

const router = createRouter({
    history: createWebHashHistory(),
    routes
})


router.beforeEach((to, from, next) => {
    //开启进度条
    start()
    //设置title
    if (to.meta.title) {
        const title = to.meta.title
        document.title = title
    }
    //auth为true放行
    if (to.meta.auth) {
        next()
        return
    }
    //如果用户信息不存在跳转登录页
    let userStr = localStorage.getItem("access-admin");
    if (!userStr) {
        next({ path: '/login' })
        return
    }
    next()
})

router.afterEach((to, from) => {
    //关闭进入条
    close()
})
export default router